// Help
//
// 保佑没Bug
//
// Created by AS on 2019/4/10.
//
// Copyright © 2018 AS. All rights reserved.
//

#import "ItemTableViewCell.h"
#import "EdgeUILabel.h"
#import "UserInfo.h"
#import "PhotosView.h"
@interface ItemTableViewCell()
/**  */
@property (nonatomic,strong) UIImageView *avatarImage;
/**  */
@property (nonatomic,strong) UIImageView *typeImage;
/**  */
@property (nonatomic,strong) UILabel *nameLabel;
/**  */
@property (nonatomic,strong) UILabel *levelLabel;
/**  */
//@property (nonatomic,strong) EdgeUILabel *tagLabel;
/**  */
//@property (nonatomic,strong) UILabel *titleLabel;
/**  */
//@property (nonatomic,strong) UIView *content_view;
/**  */
@property (nonatomic,strong) UILabel *contentLabel;
/**  */
@property (nonatomic,strong) UIImageView *createTimeImage;
/**  */
@property (nonatomic,strong) UILabel *createTimeLabel;
/**  */
@property (nonatomic,strong) UIButton *commentBtn;
/**  */
@property (nonatomic,strong) UIImageView *commentIcon;
/**  */
@property (nonatomic,strong) UILabel *commentLabel;
/**  */
@property (nonatomic,strong) UIButton *praiseBtn;
@property(nonatomic,strong) PhotosView *photoView;
/**  */
@property (nonatomic,strong) UIImageView *praiseIcon;
/**  */
@property (nonatomic,strong) UILabel *praiseLabel;

@end


@implementation ItemTableViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        [self initSubView];
    }
    return self;
}


-(void)initSubView{
    
    self.selectionStyle = UITableViewCellSelectionStyleNone;
    
    self.avatarImage = [UIImageView new];
    self.avatarImage.contentMode = UIViewContentModeScaleAspectFill;
    [self.avatarImage setLayerWithRadius:20 color:[UIColor whiteColor] width:2];
    self.avatarImage.backgroundColor = HexRGB(0xeeeeee);
    [self.contentView addSubview:self.avatarImage];
    
    self.typeImage = [UIImageView new];
    self.typeImage.contentMode = UIViewContentModeScaleAspectFit;
    [self.contentView addSubview:self.typeImage];
    
    self.nameLabel = [UILabel new];
    self.nameLabel.textColor = HexRGB(0x222222);
    self.nameLabel.font = Font14;
    [self.contentView addSubview:self.nameLabel];
    
    self.levelLabel = [UILabel new];
    self.levelLabel.textColor = HexRGB(0x888888);
    self.levelLabel.font = Font12;
    [self.contentView addSubview:self.levelLabel];

    self.contentLabel = [UILabel new];
    self.contentLabel.font = Font16;
    self.contentLabel.numberOfLines = 0;
    self.contentLabel.textColor = HexRGB(0x222222);
    [self.contentView addSubview:self.contentLabel];
    
    self.photoView = [[PhotosView alloc]init];
    [self.contentView addSubview:self.photoView];
    
    self.createTimeImage = [UIImageView new];
    self.createTimeImage.contentMode = UIViewContentModeScaleAspectFit;
    self.createTimeImage.image = [UIImage imageNamed:@"time01"];
    [self.contentView addSubview:self.createTimeImage];
    
    self.createTimeLabel = [UILabel new];
    self.createTimeLabel.textColor = HexRGB(0x888888);
    self.createTimeLabel.font = Font14;
    [self.contentView addSubview:self.createTimeLabel];
    
    self.praiseLabel = [UILabel new];
    self.praiseLabel.font = Font14;
    self.praiseLabel.textColor = HexRGB(0x888888);
    [self.contentView addSubview:self.praiseLabel];
    
    self.praiseIcon = [UIImageView new];
    [self.contentView addSubview:self.praiseIcon];
    
    self.praiseBtn = [UIButton new];
    [self.praiseBtn addTarget:self action:@selector(praiseAction) forControlEvents:UIControlEventTouchUpInside];
    [self.contentView addSubview:self.praiseBtn];
    
    self.commentLabel = [UILabel new];
    self.commentLabel.font = Font14;
    self.commentLabel.textColor = HexRGB(0x888888);
    [self.contentView addSubview:self.commentLabel];
    
    self.commentIcon = [UIImageView new];
    self.commentIcon.image = [UIImage imageNamed:@"reply"];
    [self.contentView addSubview:self.commentIcon];
    
    self.commentBtn = [UIButton new];
    [self.commentBtn addTarget:self action:@selector(replyAction) forControlEvents:UIControlEventTouchUpInside];
    [self.contentView addSubview:self.commentBtn];
    
    [self.praiseLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.contentView).offset(-12);
        make.bottom.equalTo(self.contentView).offset(-15);
    }];
    
    [self.praiseIcon mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.praiseLabel.mas_left).offset(-5);
        make.centerY.equalTo(self.praiseLabel);
        make.width.height.mas_offset(15);
    }];
    
    [self.praiseBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.equalTo(self.praiseIcon).offset(-5);
        make.bottom.equalTo(self.praiseIcon).offset(5);
        make.right.equalTo(self.praiseLabel).offset(5);
    }];
    
    [self.commentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.praiseBtn.mas_left).offset(-25);
        make.centerY.equalTo(self.praiseLabel);
    }];
    
    [self.commentIcon mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.commentLabel.mas_left).offset(-5);
        make.centerY.equalTo(self.praiseIcon).offset(2);
        make.width.height.mas_offset(15);
    }];
    
    [self.commentBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.equalTo(self.commentIcon).offset(-5);
        make.bottom.equalTo(self.commentIcon).offset(5);
        make.right.equalTo(self.commentLabel).offset(5);
    }];
    
}

-(void)replyAction{
    
    NSLog(@"回复");
    
}

-(void)praiseAction{
    
    UserInfo *user = [UserInfo getUserInfo];
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    dict[@"loginToken"] = user.loginToken;
    dict[@"id"] = @(self.modelF.model.Id);
    if (!self.modelF.model.isDz) {
        
        NSString *url = @"/api/communicationcircle/updateLike";
        [HTTPTools POST:url parameters:dict callback:^(id result, NSString *msg, NSError *error) {
            if (!error) {
                if (!msg) {
                    self.modelF.model.likeNumber += 1;
                    self.modelF.model.isDz = YES;
                    self.praiseLabel.text = [NSString stringWithFormat:@"%ld",self.modelF.model.likeNumber];
                    self.praiseIcon.image = [UIImage imageNamed:@"priseme"];
                }
            }
        }];
    }else{
        
        NSString *url = @"/api/communicationcircle/qxDz";
        [HTTPTools POST:url parameters:dict callback:^(id result, NSString *msg, NSError *error) {
            if (!error) {
                if (!msg) {
                    self.praiseIcon.image = [UIImage imageNamed:@"prise"];
                    self.modelF.model.likeNumber -= 1;
                    self.modelF.model.isDz = NO;
                    self.praiseLabel.text = [NSString stringWithFormat:@"%ld",self.modelF.model.likeNumber];
                }
            }
        }];
    }
}

-(void)setModelF:(CModelFrame *)modelF{
    _modelF = modelF;
    CModel *model = modelF.model;
    
    self.avatarImage.frame = modelF.avatarF;
    self.nameLabel.frame = modelF.nameF;
    self.typeImage.frame = modelF.typeF;
    self.levelLabel.frame = modelF.levelF;
    self.contentLabel.frame = modelF.contentF;
    self.createTimeImage.frame = modelF.timeIconF;
    
    CGSize size = [model.createTime sizeWithFont:Font12];
    
    self.createTimeLabel.frame = CGRectMake(CGRectGetMaxX(modelF.timeIconF) + 3,modelF.timeIconF.origin.y, size.width + 50, size.height);
    
    NSString *path = [PathDocument stringByAppendingPathComponent:@"ionic/www/assets/vip"];
    if (model.gradeCode - 1 > 0) {
        NSString *imagePath = [NSString stringWithFormat:@"%@/vip%ld.png",path,model.gradeCode-1];
        UIImage *image = [[UIImage alloc]initWithContentsOfFile:imagePath];
        if (!image) {
            NSLog(@"%@",imagePath);
        }else{
            self.typeImage.image = image;
        }
        self.typeImage.hidden = NO;
    }else{
        self.typeImage.hidden = YES;
    }
    
    if (model.photos.count){
        self.photoView.photos = model.photos;
        self.photoView.frame = modelF.photoViewF;
        self.photoView.hidden = NO;
    }else{
        self.photoView.hidden = YES;
    }
    
    if (model.isDz) {
        self.praiseIcon.image = [UIImage imageNamed:@"priseme"];
    }else{
        self.praiseIcon.image = [UIImage imageNamed:@"prise"];
    }
    
    [self.avatarImage sd_setImageWithURL:[NSURL URLWithString:model.photo] placeholderImage:[UIImage createImageWithColor:HexRGB(0xeeeeee)]];
    self.praiseLabel.text = [NSString stringWithFormat:@"%ld",model.likeNumber];
    self.nameLabel.text = model.nickName;
    self.levelLabel.text = model.gradeName;
    //    self.titleLabel.text = model.title;
    self.contentLabel.text = model.content;
    self.createTimeLabel.text = model.createTime;
    self.commentLabel.text = [NSString stringWithFormat:@"%ld",model.count];
}






@end
